package com.dcbeida.activity.msg;


import android.app.Activity;
import android.content.DialogInterface;
import android.support.annotation.Nullable;

import com.dcbeida.bean.MsgDetailBean;
import com.dcbeida.bean.MsgList;
import com.dcbeida.callback.DialogCallback;
import com.dcbeida.model.MsgModel;

import java.lang.reflect.Type;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by hp on 2016/5/12.
 */
public class MsgDetailPresenter implements MsgDetailContract.Present {
    private final MsgModel model;
    private MsgDetailContract.View view;
    private Activity act;

    public MsgDetailPresenter(MsgDetailContract.View view, Activity act) {
        this.view = view;
        model = MsgModel.newInstance();
        this.act = act;
    }

    //取消dialog 取消请求
    DialogInterface.OnCancelListener lis = new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            model.getCancel();
        }
    };



    
    @Override
    public void start() {

    }

    @Override
    public void getDetail(int msgId) {
        model.getDetail(msgId, new MsgDetailPresenter.GetListCallBack(act, MsgDetailBean.class, lis));


    }

    @Override
    public void confirmFeedBack(String msg, int msgId) {
        model.addNotifyFeedback(msgId,msg,new MsgDetailPresenter.FeedBackCallBack(act, Object.class, lis));
    }

    class GetListCallBack<T> extends DialogCallback<T> {

        public GetListCallBack(Activity activity, Type type, DialogInterface.OnCancelListener listener) {
            super(activity, type, listener);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                view.showDetail((MsgDetailBean) data);
            }else {
                view.showTip("没有查询到消息");
            }
        }


        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTip("数据未请求成功,请检查网络...");


        }
    }

    class FeedBackCallBack<T> extends DialogCallback<T> {

        public FeedBackCallBack(Activity activity, Type type, DialogInterface.OnCancelListener listener) {
            super(activity, type, listener);
        }

        @Override
        public void onResponse(T data) {
            view.confirmSuccess();
        }


        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTip("数据未请求成功,请检查网络...");


        }
    }

}
